$(document).ready(function() {
    //调用bootstrap的日历控件
    $('#datepicker').datepicker()
    $('#datepicker1').datepicker();
    $('.icon-calendar').bind('click',function(e){
        $(this).parent().prev().trigger('click');
        e.stopPropagation();
    });
    //jquery表单验证
    $('#myForm').validate({
        errorPlacement: function(error, element) {
            if ( element.is(":radio") )
                error.appendTo( element.parent().next().next() );
            else if ( element.is(":checkbox") )
                error.appendTo ( element.next() );
            else
                element.parent().append(error);
        }

    });

    jQuery.extend(jQuery.validator.messages, {
        required: "请填写本字段",
        date: "请输入正确的日期",
        min: "请选择离职原因",
        digits: "请输入正确的整数"
    });

    bindEvent();
    selectReason();
});

//选择离职原因
function bindEvent(){
    $('input[name="optionsRadios"]').bind('click',function(){
        hasError();
        selectReason();

        var ipt_reason = $('#iptReason'),
            sel_rea1 = $('select[name="reason1"]');

        ipt_reason.hide();
        if(sel_rea1.css('display') == 'inline-block'){
            optionItem(sel_rea1);
        }
    });
}

function selectReason(){
    var rd = $('input:radio[name=optionsRadios]:checked').val();

    var rea1 = $('#reason1'),rea2 = $('#reason2'),
        rea3 = $('#reason3');
    if(rd == 1){
        rea1.show();
        rea2.hide();
        rea3.hide();

        isShow();
    }else if(rd == 2){
        rea1.hide();
        rea2.show();
        rea3.hide();
    }else{
        rea1.hide();
        rea2.hide();
        rea3.show();
    }
}

function isShow(){
    $("#reason1").change(function(){
        var iptrea = $('#iptReason');
        iptrea.hide();
        optionItem($(this));
    });
}

function optionItem(obj){
    var iptrea = $('#iptReason');
    var opt_val = obj.val();
    iptrea.hide();
    if(opt_val == 14){
        iptrea.show();
    }
}

function hasError(){
    if($("select").hasClass('error')){
        $("select").removeClass('error');
        var lbl = $("select").next('label');
        lbl.remove();
    }
}
